package com.zx.pm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zx.common.annotation.Log;
import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.enums.BusinessType;
import com.zx.pm.domain.CompletedProject;
import com.zx.pm.service.ICompletedProjectService;
import com.zx.common.utils.poi.ExcelUtil;
import com.zx.common.core.page.TableDataInfo;

/**
 * 已完成项目信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/pm/completed")
public class CompletedProjectController extends BaseController
{
    @Autowired
    private ICompletedProjectService completedProjectService;

    /**
     * 查询已完成项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('pm:completed:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompletedProject completedProject)
    {
        startPage();
        List<CompletedProject> list = completedProjectService.selectCompletedProjectList(completedProject);
        return getDataTable(list);
    }

    /**
     * 导出已完成项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('pm:completed:export')")
    @Log(title = "已完成项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompletedProject completedProject)
    {
        List<CompletedProject> list = completedProjectService.selectCompletedProjectList(completedProject);
        ExcelUtil<CompletedProject> util = new ExcelUtil<CompletedProject>(CompletedProject.class);
        util.exportExcel(response, list, "已完成项目信息数据");
    }

    /**
     * 导入已完成项目信息数据
     */
    @PreAuthorize("@ss.hasPermi('pm:completed:import')")
    @Log(title = "已完成项目信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CompletedProject> util = new ExcelUtil<>(CompletedProject.class);
        List<CompletedProject> dataList = util.importExcel(file.getInputStream());
        int success = 0;
        for (CompletedProject item : dataList)
        {
            // 简单导入：如果带有ID且需要更新，可走更新；否则新增
            if (updateSupport && item.getId() != null)
            {
                success += completedProjectService.updateCompletedProject(item);
            }
            else
            {
                success += completedProjectService.insertCompletedProject(item);
            }
        }
        return success("导入成功" + success + "条");
    }

    /**
     * 导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CompletedProject> util = new ExcelUtil<>(CompletedProject.class);
        util.importTemplateExcel(response, "已完成项目信息数据");
    }

    /**
     * 获取已完成项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('pm:completed:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(completedProjectService.selectCompletedProjectById(id));
    }

    /**
     * 新增已完成项目信息
     */
    @PreAuthorize("@ss.hasPermi('pm:completed:add')")
    @Log(title = "已完成项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompletedProject completedProject)
    {
        return toAjax(completedProjectService.insertCompletedProject(completedProject));
    }

    /**
     * 修改已完成项目信息
     */
    @PreAuthorize("@ss.hasPermi('pm:completed:edit')")
    @Log(title = "已完成项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompletedProject completedProject)
    {
        return toAjax(completedProjectService.updateCompletedProject(completedProject));
    }

    /**
     * 删除已完成项目信息
     */
    @PreAuthorize("@ss.hasPermi('pm:completed:remove')")
    @Log(title = "已完成项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(completedProjectService.deleteCompletedProjectByIds(ids));
    }
}
